package com.mediacloud.app.newsmodule.fragment

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import com.google.android.material.bottomsheet.BottomSheetDialog
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.TextView

import com.mediacloud.app.newsmodule.R




/**
 * @author TXL
 * description :
 */
class SimpleBottomSheetDialog(context: Context,var listener:ItemClickListener?=null) : BottomSheetDialog(context,R.style.ProgressDialogStyle), View.OnClickListener {

    private var tvClose: TextView? = null
    var bitemContainer:LinearLayout?=null
    var tagData:Any?=null
    init {
        setContentView(R.layout.bts_litm)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (window != null && window!!.attributes != null) {
            val lp = window!!.attributes
            lp.width = WindowManager.LayoutParams.MATCH_PARENT
            lp.height = WindowManager.LayoutParams.MATCH_PARENT
            window!!.attributes = lp
            setTranslucentStatus()
        }
    }
    fun setTranslucentStatus() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现
            window!!.setStatusBarColor(Color.TRANSPARENT);
        } else {//4.4 全透明状态栏
            getWindow()!!.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }
    private fun initView() {
        bitemContainer=findViewById(R.id.bitemContainer)
        tvClose = findViewById(R.id.tv_close)
        tvClose?.setOnClickListener { dismiss() }
    }

    override fun onClick(v: View)
    {
        dismiss()
    }


    fun show(items:Array<String>)
    {
        initView()
        bitemContainer?.let {
            items.forEachIndexed{index,text->
                var textView=createTextView()
                textView.text = text
                it.addView(textView,createLp())
                textView.setOnClickListener {
                    dismiss()
                    listener?.itemClicked(index,tagData )
                }
            }
        }
        super.show()
    }
    fun show(itemsResId:Array<Int>)
    {
        initView()
        bitemContainer?.let {
            itemsResId.forEachIndexed{index, resId->
                var textView=createTextView()
                textView.setText(resId)
                it.addView(textView,createLp())
                textView.setOnClickListener {
                    dismiss()
                    listener?.itemClicked(index, tagData)
                }
            }
        }
        super.show()
    }
    fun createLp():LinearLayout.LayoutParams
    {
        var lp=LinearLayout.LayoutParams(-1,context.resources.getDimensionPixelOffset(R.dimen.dimen57))
        lp.gravity=Gravity.CENTER
        return lp
    }
    fun createTextView():TextView
    {
        val textView=TextView(context)
        textView.setTextColor(0xff007aff.toInt())
        textView.isFocusable=true
        textView.isClickable=true
        textView.gravity=Gravity.CENTER
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,context.resources.getDimension(R.dimen.dimen20sp))
        return textView
    }

    interface ItemClickListener
    {
        fun itemClicked(index: Int, tagData: Any?)
    }
}